<?php

class UserDao {
    
    static $usuario;
    
    static function initUsuario()
  {
     if(Config::$admRequest){
            include ('../dinamic/User.php');
        }else{
            include ('dinamic/User.php');
     }

    UserDao::$usuario = new User(null, null, null, null, null, null, null, null);
  }
    

    # FUNCTIONS TO RETRIEVE INFO - DESERIALIZE.
  public static function db_user_by_id($dbc, $id)
  {
    $query = 'SELECT * FROM user WHERE id='.$id;
    return UserDao::db_select($dbc, $query);
  }

   public static function db_user_by_login($dbc, $login)
  {
    $query = 'SELECT * FROM user WHERE login=\''.$login.'\'LIMIT 1';
    return UserDao::db_select($dbc, $query);
  }

  public static function db_user_check_login($dbc, $login, $password)
  {
    $query = 'SELECT * FROM user WHERE login=\''.$login.'\' and senha = PASSWORD(\''.$password.'\')';
    echo $query;
    return UserDao::db_select($dbc, $query);
  }

  public static function db_user_by_name($dbc, $nome)
  {
    $query = 'SELECT * FROM user WHERE nome='.$nome;
    return UserDao::db_select($dbc, $query);
  }

  protected static function db_select($dbc, $query)
  {
    if ($result = mysqli_query($dbc, $query)) {
    	if (mysqli_num_rows($result) > 0) {
	    	$row = mysqli_fetch_array($result, MYSQLI_NUM);
	    	return new User($row[0], $row[1], $row[2], $row[3],$row[4],$row[5],$row[6],$row[7]);
	    }
  	}
  }

  # FUNCTIONS TO SAVE INFO - SERIALIZE.
  public static function insert($dbc)
  {
    $query = 'INSERT INTO user VALUES (NULL,\' '.UserDao::$usuario->nome.'\',\''.UserDao::$usuario->login.'\',PASSWORD(\''. UserDao::$usuario->senha.'\'),'.
    UserDao::$usuario->create.','. UserDao::$usuario->read.','. UserDao::$usuario->update.','. UserDao::$usuario->delete.')';
    echo $query;
    $result = mysqli_query($dbc, $query);
  }

  public static function update($dbc)
  {
    if(UserDao::$usuario->senha==""){
        $query = 'UPDATE user SET nome=\''.UserDao::$usuario->nome.'\', login =\''.UserDao::$usuario->login.'\',
         user.create ='.UserDao::$usuario->create.', user.read = '.UserDao::$usuario->read.', user.update = '.UserDao::$usuario->update.',
         user.delete = '.UserDao::$usuario->delete.' WHERE id='.UserDao::$usuario->id;
    }else{
        $query = 'UPDATE user SET nome=\''.UserDao::$usuario->nome.'\', login = \''.UserDao::$usuario->login.'\'
        , senha = PASSWORD(\''.UserDao::$usuario->senha.'\'),
        user.create ='.UserDao::$usuario->create.', user.read = '.UserDao::$usuario->read.', user.update = '.UserDao::$usuario->update.',
        user.delete = '.UserDao::$usuario->delete.'    WHERE id='.UserDao::$usuario->id;
    }
    $result = mysqli_query($dbc, $query);
  }

  public static function getAll($dbc){
       $array = new ArrayObject();
       $q =  'SELECT * FROM user';

       if ($result = mysqli_query($dbc, $q)) {
             if (mysqli_num_rows($result) > 0) {
                  while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
                         $array->append(new User($row[0], $row[1], $row[2], $row[3],$row[4],$row[5],$row[6],$row[7]));
                  }
             }
       }

       return $array;
  }

  public static function deleteUserById($dbc, $id){
      $query = 'DELETE FROM user WHERE id = '.$id;
      $result = mysqli_query($dbc,$query);
  }

}
?>
